import 'package:flutter/material.dart';
import 'package:kasax/commons/commons.dart';
import 'package:kasax_demos2/entry/user_info_response_entity.dart';
import 'package:provider/provider.dart';

import 'address_respority.dart';

class AddressListProvider extends ChangeNotifier {
  String _displayText = "";

  String get displayText => _displayText;

  bool isLoadingData = false;

  List<UserInfoResponseData> dataList = [];

  /// 修改显示的文字
  changeDisplayTxt(String txt) {
    this._displayText = txt;
    notifyListeners();
  }

  /// 获取用户列表
  void fetchUserList() {
    isLoadingData = true;
    notifyListeners();
    AddressRespority.getInstance().fetchAllUser().then((value) {
      print(value);
      isLoadingData = false;
      dataList = value.data;
      notifyListeners();
    });
  }
}

class TextDisplay extends StatelessWidget {
  const TextDisplay({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var provider = Provider.of<AddressListProvider>(context);
    return Container(
      padding: EdgeInsets.all(16),
      child: Text(
        provider.displayText,
        style: TextStyle(fontSize: 24),
      ),
    );
  }
}

class TextEditWidget extends StatefulWidget {
  const TextEditWidget({Key? key}) : super(key: key);

  @override
  _TextEditWidgetState createState() => _TextEditWidgetState();
}

class _TextEditWidgetState extends State<TextEditWidget> {
  TextEditingController _textEditingController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    var provider = Provider.of<AddressListProvider>(context);

    return Container(
      child: TextField(
        controller: _textEditingController,
        decoration: InputDecoration(
            labelText: "Sample Text", border: OutlineInputBorder()),
        onChanged: (changed) => provider.changeDisplayTxt(changed),
        onSubmitted: (submitted) => provider.changeDisplayTxt(submitted),
      ),
    );
  }
}

class AddressListWidget extends StatelessWidget {
  const AddressListWidget({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    var provider = Provider.of<AddressListProvider>(context);
    return Expanded(
        child: Container(
      padding: EdgeInsets.only(left: 16, right: 16),
      child: provider.isLoadingData
          ? buildLoadingWidget()
          : provider.dataList.isEmpty
              ? Center(
                  child: Text("没有找到用户数据"),
                )
              : ListView.separated(
                  itemBuilder: (context, index) {
                    var data = provider.dataList[index];
                    String avatar = data.avatar;
                    String firstName = data.firstName;
                    return ListTile(
                      leading: avatar == null || avatar.isEmpty
                          ? Container()
                          : CircleAvatar(
                              backgroundImage: NetworkImage(data.avatar),
                            ),
                      title: firstName == null || firstName.isEmpty
                          ? Container()
                          : Text(provider.dataList[index].firstName),
                    );
                  },
                  separatorBuilder: (context, index) {
                    return Divider();
                  },
                  itemCount: provider.dataList.length),
    ));
  }
}
